﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

class CharacterCreationScreen : MenuScreen
{
    private double initialTime = 0;

    int intelligence = 1, charisma = 1, perception = 1, points = 5;

    bool edit = false;
    
    private Texture2D background;

    Day day = Day.ZERO;

    MenuEntry nameEntry, charismaEntry, intelligenceEntry, perceptionEntry, pointEntry, okEntry;

    Texture2D ball, emptyball, back, chara;

    public bool Edit
    {
        set { edit = value; }
    }

    public int Charisma
    {
        set { charisma = value; }
    }
    public int Intelligence
    {
        set { intelligence = value; }
    }
    public int Perception
    {
        set { perception = value; }
    }
    public int Points
    {
        set { points = value; }
    }

    public string Name
    {
        set { name = value; }
    }

    public Day Day
    {
        set { day = value; }
    }

    const string nameDescription = "NOME \n Representa o nome que seu personagem terá no jogo e como as outras pessoas irão chamá-lo.";
    const string charismaDescription = "CARISMA \n Usado durante as conversas com os personagens. \n Quanto maior for melhor você será tratado pelos outros personagens e mais opções de respostas você terá. \n Algumas pequenas dicas serão dadas pelos personagens se você tiver a quantidade necessária.";
    const string intelligenceDescription = "INTELIGÊNCIA \n Usado durante as interações com objetos, além de indicar o raciocínio do personagem. \n Quanto maior for maior será a quantidade de objetos que você poderá interagir de cada vez. \n Faz a ligação entre coisas ditas pelos personagens e fatos que ocorreram, liberando novas opções de falas.";
    const string perceptionDescription = "PERCEPÇÃO \n Usado durante as interações com objetos e para notar coisas durante uma conversa. \n Quanto maior for maior será a quantidade de objetos que você poderá interagir. \n Ajuda também a perceber algo não natural ou diferente em um personagem.";
    const string pointsDescription = "PONTOS \n Voce deve distribuir todos esses pontos entre os 3 atributos para poder comecar o jogo.";
    const string okDescription = "Clique aqui para começar a jogar com esse personagem. \n BOM JOGO!";

    const string nameLabel = "Nome do Personagem: ";
    string name = "Calouro";
    const string cursor = "|";
    bool cursorOn = true;

    SpriteFont font, descFont;
    SpriteFont Font
    {
        get { return font; }
        set { font = value; }
    }

    bool hasSavedGame = false;

    string nameString;
    
    public CharacterCreationScreen()
        : base()
    {
        Direction = MenuEntryDirection.VERTICAL;

        TransitionOnTime = TimeSpan.Zero;
        TransitionOffTime = TimeSpan.Zero;
        
        selectedEntry = 0;

        nameString = nameLabel + name;
    }

    private bool CheckCharacterDone()
    {
        if (name.Length < 3)
            return false;

        if( points > 0)
            return false;

        return true;
    }
    
    private void CreateMenuEntries()
    {
        //    MenuEntry nameEntry, charismaEntry, intelligenceEntry, perceptionEntry, pointEntry, okEntry;

        nameEntry = new MenuEntry(nameLabel + name);
        nameEntry.Position = new Vector2(220, 60);
        nameEntry.Description = nameDescription;
        nameEntry.DescriptionPosition = new Vector2(800, 200);
        nameEntry.DescriptionSize = 250;
        nameEntry.DescriptionFont = descFont;
        nameEntry.Font = font;
        MenuEntries.Add(nameEntry);

        charismaEntry = new MenuEntry("Carisma");
        charismaEntry.Position = new Vector2(100, 200f);
        charismaEntry.Description = charismaDescription;
        charismaEntry.DescriptionPosition = new Vector2(800, 200);
        charismaEntry.DescriptionSize = 250;
        charismaEntry.DescriptionFont = descFont;
        charismaEntry.Font = font;
        MenuEntries.Add(charismaEntry);

        intelligenceEntry = new MenuEntry("Inteligência");
        intelligenceEntry.Position = new Vector2(100, 300f);
        intelligenceEntry.Description = intelligenceDescription;
        intelligenceEntry.DescriptionPosition = new Vector2(800, 200);
        intelligenceEntry.DescriptionSize = 250;
        intelligenceEntry.DescriptionFont = descFont;
        intelligenceEntry.Font = font;
        MenuEntries.Add(intelligenceEntry);

        perceptionEntry = new MenuEntry("Percepção");
        perceptionEntry.Position = new Vector2(100, 400f);
        perceptionEntry.Description = perceptionDescription;
        perceptionEntry.DescriptionPosition = new Vector2(800, 200);
        perceptionEntry.DescriptionSize = 250;
        perceptionEntry.DescriptionFont = descFont;
        perceptionEntry.Font = font;
        MenuEntries.Add(perceptionEntry);

        pointEntry = new MenuEntry("Pontos");
        pointEntry.Position = new Vector2(200, 500f);
        pointEntry.Description = pointsDescription;
        pointEntry.DescriptionPosition = new Vector2(800, 200);
        pointEntry.DescriptionSize = 250;
        pointEntry.DescriptionFont = descFont;
        pointEntry.Font = font;
        pointEntry.IsSelectable = false;
        MenuEntries.Add(pointEntry);

        okEntry = new MenuEntry("Confirmar");
        okEntry.Position = new Vector2(700, 600f);
        okEntry.Description = okDescription;
        okEntry.DescriptionPosition = new Vector2(800, 200);
        okEntry.DescriptionSize = 250;
        okEntry.DescriptionFont = descFont;
        okEntry.Selected += EnterGame;
        okEntry.Font = font;
        MenuEntries.Add(okEntry);
    }

    public void EnterGame(object sender, EventArgs e)
    {
        EnterGame();
    }

    public void EnterGame()
    {
        ExitScreen();
        ScreenManager.AddScreen(new DaySelectionScreen(day, name, charisma, intelligence, perception));
    }

    public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
    {
        if (initialTime == 0)
            initialTime = gameTime.TotalRealTime.TotalMilliseconds;

        base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

        if (initialTime + 100 < gameTime.TotalRealTime.TotalMilliseconds)
        {
            if (!cursorOn && selectedEntry == 0)
            {
                nameString = nameLabel + name + cursor;
                cursorOn = true;
            }
            else
            {
                nameString = nameLabel + name;
                cursorOn = false;
            }

            initialTime = gameTime.TotalRealTime.TotalMilliseconds;
        }

        nameEntry.Text = nameString;

        if (CheckCharacterDone())
        {
            okEntry.IsSelectable = true;
        }
        else
        {
            okEntry.IsSelectable = false;
        }
    }

    public override void LoadContent()
    {
        base.LoadContent();

        font = ScreenManager.Game.Content.Load<SpriteFont>("SpriteFont2");
        descFont = ScreenManager.Game.Content.Load<SpriteFont>("SpriteFont1");
        back = ScreenManager.Game.Content.Load<Texture2D>("Textures/Screen/back2");
        ball = ScreenManager.Game.Content.Load<Texture2D>("Textures/Screen/ball");
        emptyball = ScreenManager.Game.Content.Load<Texture2D>("Textures/Screen/emptyball");
        chara = ScreenManager.Game.Content.Load<Texture2D>("Textures/Characters/CharDescendoParado");

        CreateMenuEntries();
    }

    public override void HandleInput()
    {
        if (InputManager.IsActionTriggered(InputManager.Action.Back))
        {
            if (edit)
                EnterGame();
            else
                Exit();

            return;
        }

        if (InputManager.IsActionTriggered(InputManager.Action.CursorLeft))
        {
            switch (selectedEntry)
            {
                case 1:
                    if (charisma > 1 && points < 5)
                    {
                        charisma--;
                        points++;
                        AudioManager.PlaySound(selectedSound);
                    }
                    break;
                case 2:
                    if (intelligence > 1 && points < 5)
                    {
                        intelligence--;
                        points++;
                        AudioManager.PlaySound(selectedSound);
                    }
                    break;
                case 3:
                    if (perception > 1 && points < 5)
                    {
                        perception--;
                        points++;
                        AudioManager.PlaySound(selectedSound);
                    }
                    break;
            }
        }

        if (InputManager.IsActionTriggered(InputManager.Action.CursorRight))
        {
            switch (selectedEntry)
            {
                case 1:
                    if (charisma < 5 && points > 0)
                    {
                        charisma++;
                        points--;
                        AudioManager.PlaySound(selectedSound);
                    }
                    break;
                case 2:
                    if (intelligence < 5 && points > 0)
                    {
                        intelligence++;
                        points--;
                        AudioManager.PlaySound(selectedSound);
                    }
                    break;
                case 3:
                    if (perception < 5 && points > 0)
                    {
                        perception++;
                        points--;
                        AudioManager.PlaySound(selectedSound);
                    }
                    break;
            }
        }

        if(selectedEntry == 0)
         CheckName();

        base.HandleInput();
    }

    private void CheckName()
    {
        if (InputManager.IsActionTriggered(InputManager.Action.BackSpace))
        {
            if (name.Length > 0)
            {
                name = name.Substring(0, name.Length - 1);
                AudioManager.PlaySound(selectedSound);
            }
        }
        else
        if (name.Length < 16)
        {
            if (InputManager.IsActionTriggered(InputManager.Action.Q)){ name += "q"; AudioManager.PlaySound(selectedSound); } else
            if (InputManager.IsActionTriggered(InputManager.Action.W)){ name += "w"; AudioManager.PlaySound(selectedSound); } else
            if (InputManager.IsActionTriggered(InputManager.Action.E)){ name += "e"; AudioManager.PlaySound(selectedSound); } else
            if (InputManager.IsActionTriggered(InputManager.Action.R)){ name += "r"; AudioManager.PlaySound(selectedSound); } else
            if (InputManager.IsActionTriggered(InputManager.Action.T)){ name += "t"; AudioManager.PlaySound(selectedSound); } else
            if (InputManager.IsActionTriggered(InputManager.Action.Y)){ name += "y"; AudioManager.PlaySound(selectedSound); } else
            if (InputManager.IsActionTriggered(InputManager.Action.U)){ name += "u"; AudioManager.PlaySound(selectedSound); } else
            if (InputManager.IsActionTriggered(InputManager.Action.I)){ name += "i"; AudioManager.PlaySound(selectedSound); } else
            if (InputManager.IsActionTriggered(InputManager.Action.O)){ name += "o"; AudioManager.PlaySound(selectedSound); } else
            if (InputManager.IsActionTriggered(InputManager.Action.P)){ name += "p"; AudioManager.PlaySound(selectedSound); } else
            if (InputManager.IsActionTriggered(InputManager.Action.A)){ name += "a"; AudioManager.PlaySound(selectedSound); } else
            if (InputManager.IsActionTriggered(InputManager.Action.S)){ name += "s"; AudioManager.PlaySound(selectedSound); } else
            if (InputManager.IsActionTriggered(InputManager.Action.D)){ name += "d"; AudioManager.PlaySound(selectedSound); } else
            if (InputManager.IsActionTriggered(InputManager.Action.F)){ name += "f"; AudioManager.PlaySound(selectedSound); } else
            if (InputManager.IsActionTriggered(InputManager.Action.G)){ name += "g"; AudioManager.PlaySound(selectedSound); } else
            if (InputManager.IsActionTriggered(InputManager.Action.H)){ name += "h"; AudioManager.PlaySound(selectedSound); } else
            if (InputManager.IsActionTriggered(InputManager.Action.J)){ name += "j"; AudioManager.PlaySound(selectedSound); } else
            if (InputManager.IsActionTriggered(InputManager.Action.K)){ name += "k"; AudioManager.PlaySound(selectedSound); } else
            if (InputManager.IsActionTriggered(InputManager.Action.L)){ name += "l"; AudioManager.PlaySound(selectedSound); } else
            if (InputManager.IsActionTriggered(InputManager.Action.Z)){ name += "z"; AudioManager.PlaySound(selectedSound); } else
            if (InputManager.IsActionTriggered(InputManager.Action.X)){ name += "x"; AudioManager.PlaySound(selectedSound); } else 
            if (InputManager.IsActionTriggered(InputManager.Action.C)){ name += "c"; AudioManager.PlaySound(selectedSound); } else
            if (InputManager.IsActionTriggered(InputManager.Action.V)){ name += "v"; AudioManager.PlaySound(selectedSound); } else
            if (InputManager.IsActionTriggered(InputManager.Action.B)){ name += "b"; AudioManager.PlaySound(selectedSound); } else
            if (InputManager.IsActionTriggered(InputManager.Action.N)){ name += "n"; AudioManager.PlaySound(selectedSound); } else
            if (InputManager.IsActionTriggered(InputManager.Action.M)){ name += "m"; AudioManager.PlaySound(selectedSound); }
        }

        if (name.Length > 0)
        {
            string first = name[0].ToString();
            first = first.ToUpper();
            name = first[0] + name.Substring(1, name.Length - 1);
        }

    }

    public override void Draw(GameTime gameTime)
    {
        SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

        spriteBatch.Begin();

        spriteBatch.Draw(back, Vector2.Zero, Color.White);

        spriteBatch.Draw(chara, new Vector2(90, 40), Color.White);

        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                switch(i)
                {
                    case 0:
                        if(j < charisma)
                            spriteBatch.Draw(ball, new Vector2(400 + 50 * j, 210 + 100 * i), Color.White);
                        else
                            spriteBatch.Draw(emptyball, new Vector2(400 + 50 * j, 210 + 100 * i), Color.White);
                        break;
                    case 1:
                        if (j < intelligence)
                            spriteBatch.Draw(ball, new Vector2(400 + 50 * j, 210 + 100 * i), Color.White);
                        else
                            spriteBatch.Draw(emptyball, new Vector2(400 + 50 * j, 210 + 100 * i), Color.White);
                        break;
                    case 2:
                        if (j < perception)
                            spriteBatch.Draw(ball, new Vector2(400 + 50 * j, 210 + 100 * i), Color.White);
                        else
                            spriteBatch.Draw(emptyball, new Vector2(400 + 50 * j, 210 + 100 * i), Color.White);
                        break;
                    case 3:
                        if (j < points)
                            spriteBatch.Draw(ball, new Vector2(400 + 50 * j, 210 + 100 * i), Color.White);
                        else
                            spriteBatch.Draw(emptyball, new Vector2(400 + 50 * j, 210 + 100 * i), Color.White);
                        break;
                }
            }
        }


        spriteBatch.End();

        base.Draw(gameTime);
    }

    public void Exit()
    {
        ExitScreen();
    }

}
